package com.example.depcontrol.controller;

import com.example.depcontrol.pojo.Emp;
import com.example.depcontrol.pojo.Result;
import com.example.depcontrol.service.EmpService;
import com.example.depcontrol.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class loginController {

    @Resource
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp)
    {
        log.info("员工登录：{}", emp);
        Emp e = empService.login(emp);

        //登陆成功，生成令牌，下发令牌
        if(e!=null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("name", e.getName());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前员工信息
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }



}
